package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.RegisterService;
import com.atguigu.spzx.model.dto.system.RegisterDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public void registerUser(RegisterDto registerDto) {
        //获取输入的用户名
        String username = registerDto.getUserName();
        //查询数据库的用户名
        SysUser sysUser = sysUserMapper.selectByUserName(username);
        if(sysUserMapper.selectByUserName(username) == null){
            String md5Password = registerDto.getPassword();
            registerDto.setPassword(DigestUtils.md5DigestAsHex(md5Password.getBytes()));
            sysUserMapper.registerUser(registerDto);
        }else{
            throw new GuiguException(ResultCodeEnum.USER_NAME_IS_EXISTS);
        }

    }
}
